#include "../global.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    vtkCreate(reader, vtkDICOMImageReader);
    reader->SetDataByteOrderToLittleEndian();
    reader->SetDirectoryName(path.toStdString().c_str());
    reader->Update();

    vtkCreate(renderer, vtkRenderer);

    double range[2];
    reader->GetOutput()->GetScalarRange(range);
    vtkCreate(pShift, vtkImageShiftScale);
    pShift->SetShift(-1.0*range[0]);
    pShift->SetScale(255.0/(range[1]-range[0]));
    pShift->SetOutputScalarTypeToUnsignedChar();
    pShift->SetInputConnection(reader->GetOutputPort());
    pShift->ReleaseDataFlagOff();
    pShift->Update();

    vtkCreate(opacityTransferFunction, vtkPiecewiseFunction);
    opacityTransferFunction->AddPoint(20, 0.0);
    opacityTransferFunction->AddPoint(255.0, 0.22);

    vtkCreate(colorTransferFunction, vtkColorTransferFunction);
    colorTransferFunction->AddRGBPoint(0.0, 0.0, 0.5, 0.0);
    colorTransferFunction->AddRGBPoint(60.0, 1.0, 0.0, 0.0);
    colorTransferFunction->AddRGBPoint(128.0, 0.2, 0.1, 0.9);
    colorTransferFunction->AddRGBPoint(196.0, 0.27, 0.21, 0.1);
    colorTransferFunction->AddRGBPoint(255.0, 0.8, 0.8, 0.8);

    vtkCreate(volumeProperty, vtkVolumeProperty);
    volumeProperty->SetColor(colorTransferFunction);
    volumeProperty->SetScalarOpacity(opacityTransferFunction);
    volumeProperty->ShadeOn();
    volumeProperty->SetInterpolationTypeToLinear();
    volumeProperty->SetAmbient(0.2);
    volumeProperty->SetDiffuse(0.9);
    volumeProperty->SetSpecular(0.2);
    volumeProperty->SetSpecularPower(10);

    vtkCreate(compositeFunction, vtkVolumeRayCastCompositeFunction);
    vtkCreate(volumeMapper, vtkVolumeRayCastMapper);
    volumeMapper->SetVolumeRayCastFunction(compositeFunction);
    volumeMapper->SetInputConnection(pShift->GetOutputPort());
    volumeMapper->SetNumberOfThreads(3);

    vtkCreate(volume, vtkVolume);
    volume->SetMapper(volumeMapper);
    volume->SetProperty(volumeProperty);

    renderer->AddVolume(volume);
    renderer->SetBackground(0, 0, 0);
    renderer->ResetCameraClippingRange();

    vtkWidgetCreate(widget, QVTKWidget);
    QVTKWidget* vtkWidget = widget.data();
    vtkWidget->GetRenderWindow()->AddRenderer(renderer);
    vtkWidget->resize(WINDOW_WIDTH, WINDOW_HEIGHT);
    vtkWidget->show();

    return a.exec();
}
